﻿package mortal.common.swfPlayer.data
{
    import com.gengine.debug.*;
    import flash.display.*;
    import flash.geom.*;
    import mortal.common.swfPlayer.frames.*;

    public class BitmapFrame extends Object
    {
        protected var _frame:int;
        public var swf:SwfFrames;
        public var x:Number = 0;
        public var y:Number = 0;
        public var width:Number = 0;
        public var height:Number = 0;
        protected var offsetX:Number = 200;
        protected var offsetY:Number = 200;
        protected var _bitmapData:BitmapData;
        private var _turnX:Number = 0;
        private var _turnY:Number = 0;
        private var _pixleNum:Number = 0;
        public static var PixleCount:Number = 0;
        private static const DefaultBitmapData:BitmapData = new BitmapData(1, 1, true, 0);

        public function BitmapFrame(param1:int)
        {
            this._frame = param1;
            return;
        }// end function

        public function get bitmapData() : BitmapData
        {
            return this.getBitmapData();
        }// end function

        private function getBitmapData() : BitmapData
        {
            var movieClip:MovieClip;
            var rect:Rectangle;
            if (this._bitmapData == null)
            {
                if (this.swf == null)
                {
                    return this._bitmapData;
                }
                movieClip = this.swf.movieClip;
                if (movieClip == null)
                {
                    return this._bitmapData;
                }
                if (movieClip.totalFrames < this._frame)
                {
                    return null;
                }
                movieClip.gotoAndStop(this._frame);
                rect = movieClip.getBounds(movieClip);
                if (rect.height == 0 || rect.width == 0)
                {
                    this._bitmapData = DefaultBitmapData;
                    var _loc_2:* = FPS.instance;
                    var _loc_3:* = FPS.instance.countFrames + 1;
                    _loc_2.countFrames = _loc_3;
                }
                else
                {
                    try
                    {
                        this._bitmapData = new BitmapData(rect.width, rect.height, true, 0);
                        this._bitmapData.draw(movieClip, new Matrix(1, 0, 0, 1, -rect.x, -rect.y), null, null, null, false);
                        this.x = rect.x - this.offsetX;
                        this.y = rect.y - this.offsetY;
                        this._pixleNum = rect.width * rect.height * 4;
                        PixleCount = PixleCount + this._pixleNum;
                        var _loc_2:* = FPS.instance;
                        var _loc_3:* = FPS.instance.countFrames + 1;
                        _loc_2.countFrames = _loc_3;
                        FPS.instance.pixleCount = PixleCount / 1024 / 1024;
                    }
                    catch (e:Error)
                    {
                        _bitmapData = null;
                    }
                }
            }
            return this._bitmapData;
        }// end function

        public function dispose(param1:Boolean = false) : void
        {
            if (this._bitmapData && this._bitmapData != DefaultBitmapData)
            {
                this._bitmapData.dispose();
                PixleCount = PixleCount - this._pixleNum;
                var _loc_2:* = FPS.instance;
                var _loc_3:* = FPS.instance.countFrames - 1;
                _loc_2.countFrames = _loc_3;
                FPS.instance.pixleCount = PixleCount / 1024 / 1024;
            }
            this._bitmapData = null;
            return;
        }// end function

        public function clone() : BitmapFrame
        {
            return new BitmapFrame(this._frame);
        }// end function

    }
}
